
var app = getApp()
Page({
  data: {
  	leftBtn:[],
  	goodsType:'',
  	goodsList:[],
  	agentId:'',
    selectIndex:'',
    noData:false
  },
  onLoad: function(options) {
  	app.getMemInfo().then(res=>{
  		this.setData({
  			agentId:res.data.agentId,
        account:res.data.id
  		})
  		this.getProductType(res.data.agentId)
  	})
  	
  },
  searchInLibrary(){
    wx.navigateTo({
      url:'searchInLibrary'
    })
  },
  //获取商品类型列表
  getProductType(agentId){
    var that = this
    var leftBtn = this.data.leftBtn
    let data = {
      agentId,
      requestType:'agent'
    }
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      let btnList = res.data.product_types
      //默认种类
      that.setData({
        goodsType:res.data.product_types[0].id
      })
      btnList.forEach(item=>{
        leftBtn.push({name:item.name,id:item.id})
        that.setData({
          leftBtn
        })
      })
      that.getGoodsList()
    })
  },
  selecttypeTap: function(event) {
    var dataSet = event.currentTarget.dataset;
    let type = dataSet.type
    var selectIndex = dataSet.index;
    var categoryId = dataSet.categoryId;//查询不同分类下的数据
    this.setData({
      selectIndex: selectIndex,
      goodsType:type
    })
    this.getGoodsList(type)
  },
  //获取商品列表
  getGoodsList(){
    wx.showLoading({
      title:'正在加载'
    })
    this.setData({
      goodsList:[]
    })
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      searchKey:''
    }
    let goodsList = []
    app.request('wx_app/product/getAllProductList',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.pageContent.forEach(item=>{
          goodsList.push({
            name:item.name,
            type:item.product_type,
            price:item.stockPrice,
            url:item.url,
            id:item.id,
            isAdded:item.isAdded
          })
          that.setData({
            goodsList
          })
        })
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
      if (that.data.goodsList.length==0) {
        that.setData({
          noData:true
        })
      }else{
        that.setData({
          noData:false
        })
      }
    })
  },
  //添加入库
  addToStock(e){
    var that = this
    let data = {
      type:e.currentTarget.dataset.type,
      account:this.data.account,
      agentId:this.data.agentId,
      productId:e.currentTarget.dataset.id
    }
    console.log(data)
    app.request('wx_app/agent/stock/add',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        that.getGoodsList()
      }
    })
  }
})